	SearchWindowsCount = 0;
	
		

			function NewSearchWindow(ServicePath,ServiceParams, ModelName)
			{
				AjaxCallStatus = GetObjects("svc/search/getemptymodel?model="+ModelName);
				AjaxCallStatus.then(function()
				{
					var XMLresp = GetLastAJAXResponse();
					var WinDivID = CreateDomForSearchWindow();
					new	SearchWindow(XMLresp,WinDivID, ModelName, ServicePath, ServiceParams);
					TurnSearchDivIntoDialog(WinDivID);
				});
				AjaxCallStatus.fail(function()
				{
					CreateErrorWindow("Error!."
							+ " Check connection with server. " +
							"Cause: " + GetLastAJAXResponse());

				});
			
			}
			
			
			function CreateDomForSearchWindow()
			{
				var WindowsID = "SearchWindow-"+SearchWindowsCount;
				$('body').append("<div id='"+WindowsID+"' title='Search window'></div>"); //un nuevo div para una nueva ventana
								
				SearchWindowsCount++;
				
				return WindowsID;
			}
			
			
			function TurnSearchDivIntoDialog(WinDivID)
			{
				//Create tooltips out of title properties of tags, with mouse track
				$("#"+WinDivID).tooltip({track: true});
				
				//Turn the dynamically generated dom into a dialog.
				$("#"+WinDivID).dialog(
					{
						height:'auto',
						width:'auto',
						buttons: 
						{
							Close: function() 
							{
								$( this ).dialog( "close" );
								$(this).remove();
							},
							
						},
						close: function(event, ui)
						{
							$(this).dialog( "close" ); //Close the Dialog
							$(this).remove();	//Remove all the appended Dom and its assocciated events
						}
				});	
				
				
			}
			
			
			function SearchWindow(XMLresp, WinDivID, ModName, ServicePath, ServiceParams)
			{

				var XMLresponse = XMLresp;
				
				
				//Hay que llevar los ID's del DOM creado Dinamicamente	
				var WindowsID = WinDivID;
				var FieldSetID = WindowsID+"-FieldSet";
				var SearchButtonID = WindowsID+-"SearchButton";
				var SvcPath = ServicePath;
				var SvcParams = ServiceParams;
				var KeysAndValues = new Array();
				var ModelName = ModName;						
				var ModelData;



				//Armamos la grilla que manipula el DOM del XML

				$("#"+WindowsID).append("<form><fieldset id='"+FieldSetID+"'></fieldset></form>");//Empezamos a crear la tabla

				//We have just one object to see the schema, so we get the first
				
				$(XMLresponse).find('object').each(function()
				{
					
					var attrCount = 0;

					ModelData = $(this);
					/***********************************************************/
					//Load all the primitives of the current object in the list,
					//and all its logic

					ModelData.find('atribute').each 
					(function()
					{


						//ID unica para cada input, para agregarle un handler especifico onTextChange

						var CurrentAttribute = $(this);
						var InputID = FieldSetID +"-Input"+attrCount;

						$("#"+FieldSetID).append("<label for='"+InputID+"'>"+CurrentAttribute.attr("name")+": </label>");
						$("#"+FieldSetID).append("<input id='"+InputID+"' value='' class='text ui-widget-content ui-corner-all'></input></br>");

						//Adding validations regarding the type of data.
						if (CurrentAttribute.attr("type")=="int" || CurrentAttribute.attr("type")=="long")
						{
							$("#"+InputID).bind
							(
									'keyup',
									function(EventArgs)
									{
										this.value = this.value.replace(/[^0-9]/gi,'');
									}
							);

						}
						else if (CurrentAttribute.attr("type")=="double" || CurrentAttribute.attr("type")=="float" )
						{
							$("#"+InputID).bind
							(
									'keyup',
									function(EventArgs)
									{
										this.value = this.value.replace(/[^0-9\.]/gi,'');
									}
							);

						}
						else if ($(this).attr("type")=="date")
						{
							$('#'+InputID).datepicker(
									{
										changeMonth: true,
										changeYear: true,
										dateFormat: 'dd/mm/yy'
									});
						}
						else if ($(this).attr("type")=="boolean")
						{
							$('#'+InputID).attr('type','checkbox');
						} 



						//The handler for each input, to form the query
						var CurrentCount = attrCount;
						$("#"+InputID).bind
						(
								'keyup',
								function(EventArgs)
								{
									var attributesCount = CurrentCount;
									var varName = CurrentAttribute.attr("name");
									var varValue = $(this).val();
									//We trim the string
									$.trim(varValue);
									if (varValue != '')
									{
										KeysAndValues[attributesCount] = varName+"="+varValue;
									}
									else
									{
										KeysAndValues[attributesCount] = null;
									}
								}
						);	

						attrCount++;
					});	


				});

				$("#"+WindowsID).append("<button id='"+SearchButtonID+"' title='Search...'></button>");

				$( "#"+SearchButtonID ).button({text: false, icons: {primary: "ui-icon-search" }}
				).click(function()
						{
					var FinalQuery = "";
					if (SvcParams.length > 1)
					{
						var ChildModelParamsKV = SvcParams.split("&");
						for (i = 0; i < ChildModelParamsKV.length; i++)
						{
							var nvPair = ChildModelParamsKV[i].split("=");
							var name = nvPair[0];
							var value = nvPair[1];

							if (name=="model") FinalQuery += name + "="+value+"&";
							if (name=="ParentChildAttribute") FinalQuery += name + "="+value+"&";
							if (name=="ParentModel") FinalQuery += name + "="+value+"&";
							if(name == "ParentModelID") FinalQuery += name + "="+value+"&";
												
						}
						
					}	
					
					FinalQuery += "Page=1&"
					
					for (i=0; i<KeysAndValues.length; i++)
					{
						if (KeysAndValues[i] != '' && KeysAndValues[i] != null && KeysAndValues[i] != undefined)
							FinalQuery+= KeysAndValues[i]+"&";
					}
					FinalQuery = FinalQuery.substring(0,(FinalQuery.length-1));

					var URL = "svc/search?"+FinalQuery;

					 NewCrudWindow("svc/search",FinalQuery, ModelName);
			            });			

			}


